package vip.idiy.anwei_android_iot.utils;

import android.content.Context;
import android.content.Intent;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;

/**
 * ClassName MyMqttCallback
 * describe:
 * Created: 勇
 * Email:118085890@qq.com
 * Version: 1.0
 * Create in 21:35 2023/4/27
 */
public class MyMqttCallback implements MqttCallback {
    private Context mContext;

    public MyMqttCallback(Context context) {
        mContext = context;
    }

    // 处理MQTT事件的回调方法
    @Override
    public void connectionLost(Throwable cause) {
        // 处理连接丢失事件
    }

    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        // 处理消息到达事件
        Intent intent = new Intent("MQTT_MESSAGE_ARRIVED");
        intent.putExtra("topic", topic);
        if(message!=null){
            intent.putExtra("message", message.toString());
        }
        mContext.sendBroadcast(intent);
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
        // 处理消息发送完成事件
    }
}